package com.gf.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 处理数组 与 List
 * 
 * @author zhuguifeng
 * 
 */
public class StrArrayHelper {

	/**
	 * 将3-4-5-6将转化数组 T[]
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static long[] strToArray1(String str, String regex) {

		if (str != null && !str.equals("")) {
			StringTokenizer tokenizer = new StringTokenizer(str, regex);
			long[] result = new long[tokenizer.countTokens()];
			int i = 0;
			while (tokenizer.hasMoreElements()) {
				long id = Long.valueOf("" + tokenizer.nextElement());
				result[i] = id;
				i++;
			}
			return result;
		}
		return new long[0];
	}

	/**
	 * 将3-4-5-6将转化数组 List<T>
	 * 
	 * @param str
	 * @param regex
	 * @return 如果为空,返回 一个空的list
	 */
	public static List<Long> strToArray2(String str, String regex) {
		List<Long> list = new ArrayList<Long>();

		if (str != null && !str.equals("")) {
			StringTokenizer tokenizer = new StringTokenizer(str, regex);

			while (tokenizer.hasMoreElements()) {
				long id = Long.valueOf("" + tokenizer.nextElement());
				list.add(id);
			}
		}
		return list;
	}
	
	/**
	 * Strings -> String list
	 * @param str
	 * @param regex
	 * @return 如果为空,返回 一个空的list
	 */
	public static List<String> strToArray3(String str,String regex){
		List<String> list = new ArrayList<String>();
		
		if(str !=null && !str.equals("")){
			StringTokenizer tokenizer = new StringTokenizer(str, regex);
			
			while (tokenizer.hasMoreElements()) {
				String object = (String) tokenizer.nextElement();
				list.add(object);
			}
		}
		return list;
	}

	/**
	 * 将list按指定的分隔符转化为str
	 * 
	 * @param list
	 * @param regex
	 * @return
	 */
	public static <T> String listToStr(List<T> list, String regex) {
		String str = "";
		if (list.size() == 0) {
			return str;
		} else if (list.size() == 1) {
			return "" + list.get(0);
		} else {
			str = "" + list.get(0);
			for (int i = 1; i < list.size(); i++) {
				str += (regex + list.get(i));
			}
			return str;
		}

	}

}
